--  查看會員交易紀錄
local httparg    = require("shared.api.httparg")
local response   = require("shared.api.response")
local config     = require("config")
local tag        = httparg.tag()
local assertion  = httparg.assertion

local cjson = require "cjson.safe"
cjson.encode_max_depth(32)
cjson.decode_max_depth(32)


local ARGS = {
    -- exist body?
    tag.body('json', 'required'),

    -- required 会自动判断是否有填，未填自动返回错误
    account =tag.json.account   ('string', assertion.non_empty_string()),
    id      =tag.json.id        ('string','required', assertion.non_empty_string())
}
do

  local result={}
  local config                       = require("config")
  local httpclient                   = require "shared.http.client"
  local headers                      = ngx.req.get_headers()
  local query                        = nil
  local util                         = require "proxy.game.common.util"
  local fill_response_header         = util.fill_response_header
  local is_match_content_type    = util.is_match_content_type
  local get_content_decoder      = util.get_content_decoder

  do
      -- add X-Forwarded-For header
      headers['X-Forwarded-For'] = ngx.var.proxy_add_x_forwarded_for
  end


  --移除方法设定
  local rmvLowKey="x-http-method-override"
  --移除gzip设定
  local rmvKey2="accept-encoding"
  for k,v in pairs(headers) do
      local low=string.lower(k)
      if low==rmvLowKey  or low==rmvKey2   then
          headers[k]=nil
      end
  end





local location = config.ENV.DATAGATE_SERVICE .. "/transaction"
local method   = "PEEK"
local bodyMap  = {}
    bodyMap.id=ARGS.id-- 交易編號
    bodyMap.account=ARGS.account-- 交易編號

    ngx.log(ngx.ERR, "交易编号===",bodyMap.id)
 local   requestData = cjson.encode(bodyMap)
    ngx.log(ngx.ERR, requestData)
 local   resp, err = httpclient.new()
                          :uri(location)
                          :headers(headers)
                          :query(query)
                          :body(requestData)
                          :send(method, 2000, nil)

    if err or (not resp) then
        ngx.status = 500
        ngx.say(err)
        ngx.exit(ngx.OK)
    end

    if not resp.body
            or not resp.headers
            or resp.status ~= 200
            or not is_match_content_type(resp, 'application/json')  then

        ngx.status = resp.status
        -- fill header except 'Server'
        fill_response_header(resp.headers)
        ngx.say(resp.body)
        ngx.exit(ngx.OK)
    end

    local respBody
    local contentDecoder = get_content_decoder(resp)
    if contentDecoder then
        respBody = contentDecoder.decode(resp.body)
    else
        -- 这儿理论上不应该出错
        ngx.status = 500
        ngx.say(err)
        ngx.exit(ngx.OK)
    end

    result={
        id=respBody.id,
        transaction_channel=respBody.transaction_channel,
        transaction_provider=respBody.transaction_provider,
        transaction_id=respBody.transaction_id,
        transaction_event=respBody.transaction_event,
        type=respBody.type,
        account=respBody.account,
        coin=respBody.coin,
        coin_amount=respBody.amount,
        balance=respBody.balance,
        create_at=respBody.create_at,
        commit_at=respBody.commit_at,
        remark=respBody.remark,

    }
    response.success(result)

end













